package commons.base.luncene;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class IndexManager {

	private static Map<Class<? extends BaseIndexService>, BaseIndexService> cache = new HashMap<Class<? extends BaseIndexService>, BaseIndexService>();
	
	static{
		// init
		try {
			addIndexService(new IndexUser());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void addIndexService(BaseIndexService indexService){
		if(!cache.containsKey(indexService.getClass())){
			cache.put(indexService.getClass(), indexService);
		}
	}
	
	@SuppressWarnings("unchecked")
	public static <T extends BaseIndexService> T getIndexService(Class<T> clazz){
		return (T)cache.get(clazz);
	}
	
	public static void shutdown(){
		for(BaseIndexService indexService : cache.values()){
			indexService.close();
		}
	}
}
